package com.cjack.frenchexcel.service;

import com.cjack.frenchexcel.dao.MooncardCardDao;
import com.cjack.frenchexcel.dao.UserAdminDao;
import com.cjack.frenchexcel.exception.BankCardExsitException;
import com.cjack.frenchexcel.exception.BankCardNotExsitException;
import com.cjack.frenchexcel.model.MooncardCardModel;
import com.cjack.frenchexcel.service.consumer.MooncardConsumer;
import com.cjack.frenchexcel.table.AdminUserTable;
import com.cjack.frenchexcel.table.MooncardCardTable;
import com.cjack.frenchexcel.util.ModelUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class MooncardCardService {

    @Autowired
    private MooncardCardDao dao;
    @Autowired
    private UserAdminDao userAdminDao;
    @Autowired
    private MooncardConsumer mooncardConsumer;

    public List<MooncardCardModel> list( Long adminUserId){
        return ModelUtils.copyListModel( dao.findByAdminUserIdOrderByCreateTimeAsc( adminUserId), MooncardCardModel.class);
    }

    public MooncardCardModel getFirstCard( Long adminUserId){
        List<MooncardCardTable> tables = dao.findByAdminUserIdOrderByCreateTimeAsc( adminUserId);
        if( tables != null && tables.size() != 0){
            return ModelUtils.copySignModel( tables.get( 0), MooncardCardModel.class);
        }
        return null;
    }

    public void save( MooncardCardModel model) throws BankCardExsitException, BankCardNotExsitException {
        //判断银行卡卡号是否已经被其他员工使用
        MooncardCardTable exsit = dao.findByBankNo( model.getBankNo());
        if( exsit != null){
            AdminUserTable user = userAdminDao.getOne( exsit.getAdminUserId());
            throw new BankCardExsitException( "银行卡卡号：[" + model.getBankNo() + "]已经被员工[" + user.getJobNumber() + "]绑定");
        }
   //     JSONObject card = getCardAccountInfo( model.getBankNo());
  //      model.setBankCardId( card.getString( "id"));
        dao.save( ModelUtils.copySignModel( model, MooncardCardTable.class));
        AdminUserTable user = userAdminDao.getOne( model.getAdminUserId());
    //    user.setUserProfileId( card.getString( "user_profile_id"));
        userAdminDao.save( user);
    }

    public void del( Long id){
        dao.delete( id);
    }

    public void delByBankNo( String bankNo) throws BankCardNotExsitException {
        MooncardCardTable exsit = dao.findByBankNo( bankNo);
        if( exsit == null){
            throw new BankCardNotExsitException( "银行卡卡号：[" + bankNo + "]不存在");
        }
        dao.delete( exsit);
    }

    /**
     * 查询bankcard对应的在mooncard的cardid
     * @param bankCard
     * @return
     * @throws BankCardNotExsitException
     */
    private JSONObject getCardAccountInfo( String bankCard) throws BankCardNotExsitException {
        JSONArray accounts = mooncardConsumer.cards();
        if( accounts != null && accounts.size() > 0){
            for( int i = 0 ; i < accounts.size() ; i++){
                JSONObject account = accounts.getJSONObject( i);
                //这里的token即是该员工的银行卡号
                if( account.getString( "token").equals( bankCard)){
                    return account;
                }
            }
        }
        throw new BankCardNotExsitException( "银行卡号[" + bankCard + "]对应的Card在Mooncard系统中不存在，保存失败");
    }
}
